package com.example.myapplication5;
import android.animation.AnimatorInflater;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.animation.TypeEvaluator;
import android.animation.ValueAnimator;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.animation.LinearInterpolator;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class AnimatorActivity extends AppCompatActivity implements View.OnClickListener {
    private Button mButton;
    private ImageView mImage;
    private TextView mText;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_animator);
        mButton = findViewById(R.id.button);
        mImage = findViewById(R.id.image);
        mText = findViewById(R.id.text);
        mButton.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.button) {
            //initAnimator();
            //initValueAnimator2();
            //initValueAnimator3();
            //initValueAnimator4();
            //initObjectAnimator();
            //initObjectAnimator2();
            initObjectAnimator3();
        }
    }

    private void initAnimator() {
        ValueAnimator valueAnimator = (ValueAnimator) AnimatorInflater.loadAnimator(this, R.animator.animator);
        valueAnimator.setTarget(mImage);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(@NonNull ValueAnimator animation) {
                float value = (float) animation.getAnimatedValue();
                Log.e("test123", "-----------------------------" + value);
                //mImage.setAlpha(value);
                //mImage.setRotation(value * 360);
                //mImage.setRotationX(value * 360);
                //mImage.setRotationY(value * 360);
                //mImage.setScaleX(value);
                //mImage.setScaleY(value);
                //mImage.getLayoutParams().width = (int) (mImage.getLayoutParams().width * value);
                //mImage.requestLayout();
                mImage.setTranslationX(value * 100);
                mImage.setTranslationY(value * 200);
            }
        });
        valueAnimator.setInterpolator(new LinearInterpolator());
        valueAnimator.start();
    }
    private void initValueAnimator2() {
        ValueAnimator valueAnimator = ValueAnimator.ofFloat(1F, 0, 1, 0, 0.5f, 0.2f);
        valueAnimator.setDuration(6000);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(@NonNull ValueAnimator animation) {
                float value = (float) animation.getAnimatedValue();
                Log.e("test123", "-----------------------------" + value);
                //mImage.setAlpha(value);
                //mImage.setTranslationY(value * 120);
                mImage.setRotation(value * 360);
            }
        });
        valueAnimator.setInterpolator(new LinearInterpolator());
        valueAnimator.start();
    }

    private void initValueAnimator3() {
        ValueAnimator valueAnimator = ValueAnimator.ofArgb(
                0xFFFF0000, 0xFF00FF00, 0xFF0000FF, 0xFF000000, 0xFFFFFFFF);
        valueAnimator.setDuration(4000);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(@NonNull ValueAnimator animation) {
                int color = (int) animation.getAnimatedValue();
                mText.setBackgroundColor(color);
            }
        });
        valueAnimator.setInterpolator(new LinearInterpolator());
        valueAnimator.start();
    }

    private void initValueAnimator4() {
        Man boy = new Man("小王", 12);
        Man old = new Man("老王", 100);
        ValueAnimator valueAnimator = ValueAnimator.ofObject(new TypeEvaluator() {
            @Override
            public Object evaluate(float fraction, Object startValue, Object endValue) {
                Man boy = (Man) startValue;
                Man old = (Man) endValue;
                int age = (int) (boy.getAge() + (old.getAge() - boy.getAge()) * fraction);
                String name;
                if (age < 30) {
                    name = "小王";
                } else if (age < 60) {
                    name = "王师傅";
                } else {
                    name = "老王";
                }
                Man man = new Man(name, age);
                return man;
            }
        }, boy, old);

        valueAnimator.setDuration(6000);
        valueAnimator.setInterpolator(new LinearInterpolator());
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(@NonNull ValueAnimator animation) {
                Man man = (Man) animation.getAnimatedValue();
                mText.setText(man.toString());
            }
        });
        valueAnimator.start();
    }

    private void initObjectAnimator() {
//        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(
//                mText, "translationY", 100, -200, 100, -300, 1);
//        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(
//                mImage, "alpha", 1, 0, 1, 0, 1);
        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(
                mImage, "rotationX", 0, 360, 0, -360, 0);
        objectAnimator.setInterpolator(new LinearInterpolator());
        objectAnimator.setDuration(3000);
        objectAnimator.start();
    }

    private void initObjectAnimator2() {
        Man man = new Man("老张", 1);
        ObjectAnimator objectAnimator = ObjectAnimator.ofInt(man, "age", 1, 100);
        objectAnimator.setDuration(4000);
        objectAnimator.setInterpolator(new LinearInterpolator());
        objectAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(@NonNull ValueAnimator animation) {
                mText.setText(man.toString());
            }
        });
        objectAnimator.start();
    }

    private void initObjectAnimator3() {
        PropertyValuesHolder alphaHolder = PropertyValuesHolder.ofFloat("Alpha", 1, 0, 1, 0, 1, 0, 1);
        PropertyValuesHolder rotationHolder = PropertyValuesHolder.ofFloat("Rotation", 0, 360, 0, -360, 0);
        PropertyValuesHolder rotationXHolder = PropertyValuesHolder.ofFloat("RotationX", 0, 360, 0, 360, 0);
        PropertyValuesHolder rotationYHolder = PropertyValuesHolder.ofFloat("RotationY", 0, 360, 0, 360, 0);
        PropertyValuesHolder colorHolder = PropertyValuesHolder.ofInt("BackgroundColor", 0xFFFF0000, 0xFF00FF00, 0xFF0000FF, 0xFFFFFFFF, 0xFF000000);
        PropertyValuesHolder scaleXHolder = PropertyValuesHolder.ofFloat("ScaleX", 1, 2, 1, 2, 1, 2, 1);
        PropertyValuesHolder scaleYHolder = PropertyValuesHolder.ofFloat("ScaleY", 1, 2.2F, 1.3F, 2.4F, 1.0F, 2, 0.3F, 1.8F, 1);
        PropertyValuesHolder translationXHolder = PropertyValuesHolder.ofFloat("translationX", 0, 200, 0, -200, 0, 222, 0);
        PropertyValuesHolder translationYHolder = PropertyValuesHolder.ofFloat("translationY", 0, 300, 0, -300, 0, 222, 0);

        ObjectAnimator objectAnimator = ObjectAnimator.ofPropertyValuesHolder(mImage,
                rotationHolder, translationXHolder, translationYHolder, scaleXHolder, scaleYHolder, rotationXHolder);
        objectAnimator.setDuration(5000);
        objectAnimator.setInterpolator(new LinearInterpolator());
        objectAnimator.start();
    }
}